/*
 * Copyright 2009 Lukasz Wozniak
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *
 * http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package pl.sind.blip.entities;

import org.json.JSONException;
import org.json.JSONObject;

public class Subscription extends Entity {

	private String trackedUserPath;
	private String trackingUserPath;
	private Transport transport;

	public Subscription() {
		super();
	}

	public Subscription(String trackedUserPath, String trackingUserPath,
			Transport transport) {
		super();
		this.trackedUserPath = trackedUserPath;
		this.trackingUserPath = trackingUserPath;
		this.transport = transport;
	}

	public Subscription(JSONObject jsonObject) throws JSONException {

		if (jsonObject.has("transport")) {
			transport = new Transport(jsonObject.getJSONObject("transport"));
		}
		trackedUserPath = jsonObject.optString("tracked_user_path");
		trackingUserPath = jsonObject.optString("tracking_user_path");

	}

	public String getTrackedUserPath() {
		return trackedUserPath;
	}

	public void setTrackedUserPath(String trackedUserPath) {
		this.trackedUserPath = trackedUserPath;
	}

	public String getTrackingUserPath() {
		return trackingUserPath;
	}

	public void setTrackingUserPath(String trackingUserPath) {
		this.trackingUserPath = trackingUserPath;
	}

	public Transport getTransport() {
		return transport;
	}

	public void setTransport(Transport transport) {
		this.transport = transport;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((trackedUserPath == null) ? 0 : trackedUserPath.hashCode());
		result = prime
				* result
				+ ((trackingUserPath == null) ? 0 : trackingUserPath.hashCode());
		// ignore transport
		// result = prime * result
		// + ((transport == null) ? 0 : transport.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Subscription other = (Subscription) obj;
		if (trackedUserPath == null) {
			if (other.trackedUserPath != null)
				return false;
		} else if (!trackedUserPath.equals(other.trackedUserPath))
			return false;
		if (trackingUserPath == null) {
			if (other.trackingUserPath != null)
				return false;
		} else if (!trackingUserPath.equals(other.trackingUserPath))
			return false;
		// ignore transport
		// if (transport == null) {
		// if (other.transport != null)
		// return false;
		// } else if (!transport.equals(other.transport))
		// return false;
		return true;
	}

	@Override
	public String toString() {
		StringBuilder b = new StringBuilder();
		b.append(getClass().getName()).append("{");
		b.append("trackedUserPath: ").append(trackedUserPath);
		b.append(", trackingUserPath: ").append(trackingUserPath);
		b.append(", transport: ").append(transport);
		b.append("}");
		return b.toString();
	}

}
